﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class BotTrust
    {
        public BotTrust()
        {
            int testCount = Int32.Parse(TestConsole.ReadLine());

            for (int testCase = 1; testCase <= testCount; testCase++)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int jobCount = Int32.Parse(input[0]);
                int[] before = new int[jobCount + 1];
                int[] delay = new int[jobCount + 1];
                int[] position = new int[jobCount + 1];
                int[] time = new int[jobCount + 1];
                bool[] orange = new bool[jobCount + 1];
                int beforeOrange = 0, beforeBlue = 0;

                position[0] = 1;
                for (int i = 1; i <= jobCount; i++)
                {
                    orange[i] = input[i * 2 - 1].Trim() == "O";
                    position[i] = Int32.Parse(input[i * 2]);
                    if (orange[i])
                    {
                        before[i] = beforeOrange;
                        beforeOrange = i;
                    }
                    else
                    {
                        before[i] = beforeBlue;
                        beforeBlue = i;
                    }
                    delay[i] = Math.Abs(position[before[i]] - position[i]) + 1;
                }

                time[1] = delay[1];
                for (int i = 2; i <= jobCount; i++)
                {
                    if (orange[i] ^ orange[i - 1]) //other color
                    {
                        time[i] = Math.Max(time[i - 1] + 1, time[before[i]] + delay[i]);
                    }
                    else
                    {
                        time[i] = time[i - 1] + delay[i];
                    }
                }
                Console.WriteLine(string.Format("Case #{0}: {1}", testCase, time[jobCount]));
            }
        }
    }
}
